package chapter09.collection;

import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;

/**
 * @Author: insight
 * @Description: 链表的学习
 * @LastDate: 2021/1/23
 */
public class Test01_LinkedList {
    public static void main(String[] args) {
        var list1 = new LinkedList<Character>();
        var list2 = new LinkedList<Character>();
        for (int i = 0; i < 6; i+=2) {
            list1.add((char)('A' + i));
        }
        for (int i = 0; i < 6; i+=2) {
            list2.add((char)('B' + i));
        }
        list2.add('G');
        System.out.println("List1: " + list1);
        System.out.println("List2: " + list2);

        //合并list
        var it1 = list1.listIterator();
        var it2 = list2.iterator();
        while (it2.hasNext()) {
            if (it1.hasNext()) {
                it1.next();
            }
            it1.add(it2.next());
        }
        System.out.println("After merge List1: " + list1);

        //list2 每隔一个删除一个
        it2 = list2.iterator();
        while (it2.hasNext()) {
            it2.next();
            if (it2.hasNext()) {
                it2.next();
                it2.remove();
            }
        }
        System.out.println("After remove List2: " + list2);

        list1.removeAll(list2);
        System.out.println("Remove all words in List2 from List1: " + list1);
    }

    public static void listiteratorTest(@NotNull LinkedList<Integer> list) {
        //表头添加元素变成新表头
        var it = list.listIterator();
        //注意是在迭代器之前添加元素
        it.add(0);
        System.out.print("添加新表头：");
        System.out.println(list);
        //删除旧表头
        it.next();
        it.remove();
        System.out.print("删除旧表头：");
        System.out.println(list);
        //遍历到末尾
        while (it.hasNext()) {
            it.next();
        }
        //添加新表尾
        it.add(0);
        System.out.print("添加新表尾：");
        System.out.println(list);
        //向前移动两步 删除旧表尾
        it.previous();
        it.previous();
        it.remove();
        System.out.print("删除旧表尾：");
        System.out.println(list);
    }

    @NotNull
    public static LinkedList<Integer> generateIntegerList() {
        var list = new LinkedList<Integer>();
        for (int i = 0; i < 3; i++) {
            list.add(i + 1);
        }
        System.out.println(list);
        return list;
    }

    public static void printIntegerList(@NotNull LinkedList<Integer> list) {
        for (Integer integer : list) {
            System.out.print(integer + " ");
        }
        System.out.println();
    }

    public static void addBeforeIterator(@NotNull LinkedList<Integer> list) {
        //使用listIterator 在迭代器之前添加
        var it = list.listIterator();
        it.next();
        it.add(0);
        System.out.println(list);
    }

    public static void deleteSecond(@NotNull LinkedList<Integer> list) {
        //删除第二个元素
        var it = list.iterator();
        it = list.listIterator();
        var first = it.next();
        var second = it.next();
        it.remove();
        System.out.println(list);
    }
}